package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private MovementService movementService;

    @Reference
    private CommentService commentService;

    @Reference
    private VideoService videoService;

    // 分页查询用户列表
    public ResponseEntity findUserInfoByPage(Integer pageNum, Integer pageSize) {
        // 1.调用rpc
        PageBeanVo pageBeanVo = userInfoService.findUserInfoByPage(pageNum, pageSize);

        // 2.返回
        return ResponseEntity.ok(pageBeanVo);
    }

    // 用户详情
    public ResponseEntity findUserInfoById(Long userId) {
        // 1.调用rpc
        UserInfo userInfo = userInfoService.findByUserId(userId);

        // 2.返回
        return ResponseEntity.ok(userInfo);
    }

    // 动态列表
    public ResponseEntity findMovementByPage(Long uid, Integer state, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = movementService.findMovementByUserIdAndState(uid, state, pageNum, pageSize);

        // 2.封装vo
        List<MovementVo> voList = new ArrayList<>();

        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                // 创建vo
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfoService.findByUserId(movement.getUserId())); // 用户信息
                vo.setMovement(movement); // 用户动态
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated()))); // 发布时间
                // 添加到集合
                voList.add(vo);
            }
        }

        // 3.重置vo到分页对象
        pageBeanVo.setItems(voList);
        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);

    }

    // 动态详情
    public ResponseEntity findMovementById(String movementId) {
        // 1.调用rpc查询movement
        Movement movement = movementService.findMovementById(new ObjectId(movementId));
        // 2.封装vo
        MovementVo vo = new MovementVo();
        vo.setUserInfo(userInfoService.findByUserId(movement.getUserId())); // 用户信息
        vo.setMovement(movement); // 用户动态
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated()))); // 发布时间
        // 3.返回vo
        return ResponseEntity.ok(vo);
    }

    // 评论列表
    public ResponseEntity findCommentVoByPage(String messageID, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询评论列表
        PageBeanVo pageBeanVo = commentService.findCommentByPage(messageID, 2, pageNum, pageSize);
        // 2.封装commentVo
        List<CommentVo> voList = new ArrayList<>();
        // 2-1 取出comment列表
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 创建vo
                CommentVo commentVo = new CommentVo();
                // 设置userInfo
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
                commentVo.setAvatar(userInfo.getAvatar()); // 头像
                commentVo.setNickname(userInfo.getNickname()); // 昵称
                // 设置comment
                commentVo.setId(comment.getPublishId().toString()); // 评论id
                commentVo.setContent(comment.getContent()); // 评论内容
                commentVo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 评论时间 2021-06-16 12:12:15

                // 添加到集合中
                voList.add(commentVo);
            }
        }
        // 3.重置vo到分页对象
        pageBeanVo.setItems(voList);

        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 查询用户视频列表
    public ResponseEntity findMyVideoVoByPage(Long uid, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = videoService.findMyVideByPage(uid, pageNum, pageSize);
        // 2.封装vo
        List<VideoVo> voList = new ArrayList<>();
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                // 查询视频发布人的userInfo
                UserInfo userInfo = userInfoService.findByUserId(video.getUserId());
                // 创建vo
                VideoVo vo = new VideoVo();
                // 设置用户信息
                vo.setUserId(userInfo.getId());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());

                vo.setId(video.getId().toString()); // id必须设置，小视频点赞需要它~~~
                vo.setCover(video.getPicUrl());  // 视频封面
                vo.setVideoUrl(video.getVideoUrl()); // 视频连接
                vo.setSignature(video.getText()); // 视频内容
                vo.setLikeCount(video.getLikeCount()); // 点赞数量
                vo.setCommentCount(video.getCommentCount()); // 评论数量

                // 添加到list
                voList.add(vo);
            }
        }

        // 3.重置vo到分页对象
        pageBeanVo.setItems(voList);

        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }
}
